<?php
/*
 * @Author: your name
 * @Date: 2021-09-20 23:28:47
 * @LastEditTime: 2022-02-22 22:23:23
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: /nlog/app/Exceptions/Handler.php
 */

// namespace App\Exceptions;

// use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
// use Throwable;


namespace App\Exceptions;

use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Throwable;
// use Illuminate\Routing\Router;
// use Illuminate\Contracts\Support\Responsable;
// use Illuminate\Http\Exceptions\HttpResponseException;
// use Illuminate\Auth\AuthenticationException;
// use Illuminate\Validation\ValidationException;

class Handler extends ExceptionHandler
{
    /**
     * A list of the exception types that are not reported.
     *
     * @var array
     */
    protected $dontReport = [
        //
    ];

    /**
     * A list of the inputs that are never flashed for validation exceptions.
     *
     * @var array
     */
    protected $dontFlash = [
        'current_password',
        'password',
        'password_confirmation',
    ];

    /**
     * Register the exception handling callbacks for the application.
     *
     * @return void
     */
    public function register()
    {
        $this->reportable(function (Throwable $e) {
            //
        })->stop();  //取消laravel自带的日志功能
    }



    /**
     * 重写异常
     * @param \Illuminate\Http\Request $request
     * @param Throwable $e
     * @return \Illuminate\Http\JsonResponse|\Symfony\Component\HttpFoundation\Response
     * @throws \ReflectionException
     */
    public function render($request, Throwable $e)
    {

        // if (method_exists($e, 'render') && $response = $e->render($request)) {

        //     return Router::toResponse($request, $response);
        // } elseif ($e instanceof Responsable) {

        //     return $e->toResponse($request);
        // }

        // $e = $this->prepareException($this->mapException($e));

        // foreach ($this->renderCallbacks as $renderCallback) {
        //     if (is_a($e, $this->firstClosureParameterType($renderCallback))) {
        //         $response = $renderCallback($e, $request);

        //         if (! is_null($response)) {
        //             return $response;
        //         }
        //     }
        // }

        // if ($e instanceof HttpResponseException) {

        //     return $e->getResponse();
        // } elseif ($e instanceof AuthenticationException) {

        //     return toJson(403,$e->getMessage());
        //     // return $this->unauthenticated($request, $e);
        // } elseif ($e instanceof ValidationException) {

        //     return $this->convertValidationExceptionToResponse($e, $request);
        // }
        //    var_dump($_POST[]);
        // var_dump($e->statusCode());
        // print_r( $e->getCode());
        // if ($e instanceof HttpResponseException) {
        //     var_dump($e);
        // }
        $error = $this->convertExceptionToResponse($e);
        $statusCode = $error->getStatusCode();   //获得状态码
        if ($statusCode == 404) {
            $returnMessage = '资源不存在~';
        } else if ($statusCode == 400) {
            $returnMessage = '请求失败,缺少参数';
        } else {
            $returnMessage =  $e->getMessage() ;    //是否开启错误返回 线上不开启
        }
        // var_dump($e->getMessage());
        return toJson($statusCode, $returnMessage, [], $statusCode, true);


        //        return $request->expectsJson()
        //            ? $this->prepareJsonResponse($request, $e)
        //            : $this->prepareResponse($request, $e);
    }
}
